home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / menu-new.tar / menu-new / menu / appl.c next >
C/C++ Source or Header  |  1993-04-08  |  13KB  |  695 lines

  1. # include <ctype.h>
  2. # include "utils.h"
  3. # include "menu.h"
  4.  
  5. char *do_selection(int);
  6.  
  7. int check_file();
  8.  
  9. /*    PSC MENU COPYRIGHT NOTICE
  10.  
  11.     Part of PSCMenu
  12.  
  13.     This software is to be considered to be public domain, it
  14. may be copied, modified and parts of it may be used in other programs
  15. as long as this copyright notice remains intact.
  16.  
  17.     Copyright()   PSC - Plymouth State College
  18.     Written by:   Ted Wisniewski 12-9-1990
  19.  
  20. */
  21.  
  22. /*
  23.  *    void box_screen():
  24.  *
  25.  *    Parameters:    None.
  26.  *
  27.  *    Purpose:    Draw a box around the perimeter of the screen 
  28.  *            in reverse video mode.
  29.  *
  30.  *    Returns:    None.
  31.  *
  32.  *    Last Modify:    01-12-91 (TW)
  33.  *
  34.  */
  35.  
  36. void dir_set(menu_ent *menu)
  37. {
  38.     char buf[LINESZ];
  39.     
  40.     if (menu->execdir[0]!='\0')
  41.     {
  42.         getcwd(buf,LINESZ);
  43.         chdir(menu->execdir);
  44.         strcpy(menu->execdir,buf);
  45.     }
  46. }
  47.  
  48. void box_screen()
  49. {
  50.     int i;
  51.     char buf[256];
  52.  
  53.     Sprintf(buf,"%80s"," ");
  54.     start_rev();
  55.     move_csr(0,FIRST_LINE);
  56.     print_str(buf);
  57.     move_csr(0,STATUS_LINE);
  58.     print_str(buf);
  59.     for(i=1;i<22;i++){
  60.        move_csr(0,i);
  61.        outc(' ');
  62.        move_csr(79,i);
  63.        outc(' ');
  64.     }
  65.     end_rev();
  66. }
  67.  
  68. /*
  69.  *    void disp_menu():
  70.  *
  71.  *    Parameters:    menu -  a pointer to the menu information
  72.  *            n_ent - The number of entries in the menu.
  73.  *
  74.  *    Purpose:    Display the Menu on the screen.
  75.  *
  76.  *    Returns:    None.
  77.  *
  78.  *    Last Modify:    01-12-91 (TW)
  79.  *
  80.  */
  81.  
  82. void disp_menu(menu,n_ent)
  83. menu_ent *menu;
  84. int n_ent;
  85. {
  86.     int ind = 0;
  87.     char tmp[5];
  88.     
  89.     while(ind < n_ent){
  90.        if(ind == 0){
  91.          move_csr((CENTER-(strlen(menu[0].desc)/2)),(ind*2) + 3);
  92.        }else{
  93.          if(ind > 0){
  94.            move_csr(NUMCOL,(ind*2) + 4);
  95.            Sprintf(tmp,"%d",ind);
  96.            print_str(tmp);
  97.          }
  98.          move_csr(DESCOL,(ind*2) + 4);
  99.        }
  100.        hi_lite(menu[ind].desc);
  101.        ind++;
  102.     }
  103. }
  104.  
  105. /*
  106.  *    static do_help():
  107.  *
  108.  *    Parameters:    menu - pointer to menu information.
  109.  *            num  - the number of entries in the menu.
  110.  *    
  111.  *    Purpose:    Show help on a menu option.
  112.  *
  113.  *    Returns:    None.
  114.  *
  115.  *    Last Modify:    01-12-91 (TW)
  116.  *
  117.  */
  118.  
  119. static do_help(menu,num)
  120. menu_ent *menu;
  121. int num;
  122. {
  123.     char buff[256];
  124.  
  125.      Sprintf(buff,"cat %s",menu[num].help_fil);
  126.      exec_pipe(buff);
  127. }
  128.  
  129. /*
  130.  *    void do_menu():
  131.  *
  132.  *    Parameters:     menu  - pointer to menu information.
  133.  *            n_ent - number of menu entries.
  134.  *    
  135.  *    Purpose:    Main work loop, keystrokes interpreted.
  136.  *
  137.  *    Returns:    None
  138.  *
  139.  *    Last Modify:    01-12-91 (TW)
  140.  *
  141.  */
  142.  
  143. void do_menu(menu,n_ent)
  144. menu_ent *menu;
  145. int n_ent;
  146. {
  147.     char buff[LINESZ], inp = '\0', save = '\0', *ptr;
  148.     int tmp, num;
  149.  
  150.     move_csr(4,20);
  151.     while(inp != 'q') {
  152.        switch (inp) {
  153.             case 'c':
  154.           erase_menu(n_ent);
  155.               change();
  156.               disp_menu(menu,n_ent);
  157.             break;
  158.         case 'x':
  159.           erase_menu(n_ent);
  160.           exec_it();
  161.               disp_menu(menu,n_ent);
  162.         break;
  163.         case 'd':
  164.           erase_menu(n_ent);
  165.               exec_cshell("ls",CONTINUE);
  166.               disp_menu(menu,n_ent);
  167.             break;
  168.         case 'P':
  169.           erase_menu(n_ent);
  170.           do_printers();
  171.           disp_menu(menu,n_ent);
  172.         break;
  173.         case '?':
  174.           Sprintf(buff,"Get help on item %d..%d? ",1,n_ent-1);
  175.           move_csr(25,20);
  176.           hi_lite(buff);
  177.           num = (my_getchar() - '0');
  178.              erase_line(25,20,30);
  179.           if((num > 0) && (num < n_ent)){
  180.              if((check_file(menu[num].help_fil)) == -1){
  181.                    move_csr(23,23);
  182.                    hi_lite("Sorry, There is no help file for this.");
  183.                   continue_it(20);
  184.                    erase_line(23,23,40);
  185.              }else{
  186.                 erase_menu(n_ent);
  187.                 do_help(menu,num);
  188.                     disp_menu(menu,n_ent);
  189.              }
  190.           }
  191.         break;
  192.         case 'l':
  193.           erase_menu(n_ent);
  194.               exec_pipe("ls -l");
  195.               disp_menu(menu,n_ent);
  196.         break;
  197.         case 12:
  198.         case RE_DRAW:
  199.             clr_scr();
  200.           setup_screen();
  201.           disp_menu(menu,n_ent);
  202.           Fflush(stdin);
  203.         break;
  204.             case 'm':
  205.           if(m_flag != TRUE){
  206.             erase_menu(n_ent);
  207.               (void) read_menu(menu,MAIN_MENU,&n_ent);
  208.               disp_menu(menu,n_ent);
  209.             m_flag = TRUE;
  210.           }
  211.             break;
  212.         case 'L':
  213.           clr_scr();
  214.           move_csr(0,LAST_LINE);
  215.           reset_tty();
  216.           log_out();
  217.         break;
  218.             case 'p':
  219.         case LF:
  220.         case RETURN:
  221.            if(m_flag != TRUE)
  222.               inp = n_ent-1 + '0';
  223.         default:
  224.               tmp = inp - '0';
  225.               if((tmp >= 1) && (tmp < n_ent)){
  226.                 switch(menu[tmp].key){
  227.                    case '+':
  228.               erase_menu(n_ent);
  229. #define dirset(num) dir_set(&menu[tmp]) 
  230.               dirset(tmp);
  231.                 exec_pipe(menu[tmp].cmd);
  232.               dirset(tmp);
  233.                 disp_menu(menu,n_ent);
  234.                    break;
  235.                    case '%':
  236.               dirset(tmp);
  237.               exec_cshell(menu[tmp].cmd,NO_CONTINUE);
  238.               dirset(tmp);
  239.                 disp_menu(menu,n_ent);
  240.                    break;
  241.                    case '*':
  242.               dirset(tmp);
  243.               exec_cshell(menu[tmp].cmd,CONTINUE);
  244.               dirset(tmp);
  245.                 disp_menu(menu,n_ent);
  246.                    break;
  247.                case '@': menu->key = '@';
  248.                    case '&':
  249.               erase_menu(n_ent);
  250.                 (void) read_menu(menu,menu[tmp].cmd,&n_ent);
  251.                 disp_menu(menu,n_ent);
  252.                    break;
  253.                    case '1':
  254.               erase_menu(n_ent);
  255.               dirset(tmp);
  256.               One_fname(buffer,2);
  257.               if((strlen(buffer)) != 0){
  258. #define GetArgs(buffer) if (!strstr(menu[tmp].cmd,"%s")) Sprintf(string,"%s %s",menu[tmp].cmd,buffer); \
  259. else Sprintf(string,menu[tmp].cmd,buffer);
  260.                GetArgs(buffer);
  261.               /*  Sprintf(string,"%s %s",menu[tmp].cmd,buffer); */
  262.                 exec_cshell(string,CONTINUE);
  263.                 clear_array(string);
  264.               }else
  265.                 erase_line(2,2,40);
  266.               dirset(tmp);
  267.                 disp_menu(menu,n_ent);
  268.                    break;
  269.                    case '2':
  270.               erase_menu(n_ent);
  271.               dirset(tmp);
  272.               One_fname(buffer,2);
  273.               if((strlen(buffer)) != 0){
  274.                 GetArgs(buffer);
  275.                 /* Sprintf(string,"%s %s ",menu[tmp].cmd,buffer); */
  276.                 Two_fname(buffer,3);
  277.                 if((strlen(buffer)) != 0){
  278.                   if(!isprint(buffer[strlen(buffer)]))
  279.                      rm_lf(buffer);
  280.                   Strcat(string," ");
  281.                   Strcat(string,buffer);
  282.                   exec_cshell(string,CONTINUE);
  283.                   clear_array(string);
  284.                 }else{
  285.                   erase_line(2,2,40);
  286.                   erase_line(2,3,40);
  287.                 }
  288.               }else
  289.                 erase_line(2,2,40);
  290.               dirset(tmp);
  291.                 disp_menu(menu,n_ent);
  292.                    break;
  293.                    case '3':
  294.               erase_menu(n_ent);
  295.               dirset(tmp);
  296.               ask_who(buffer,2);
  297.               if((strlen(buffer)) != 0){
  298.                 GetArgs(buffer);
  299.                 /* Sprintf(string,"%s %s",menu[tmp].cmd,buffer); */
  300.                 exec_cshell(string,CONTINUE);
  301.                 clear_array(buffer);
  302.                 clear_array(string);
  303.               }else
  304.                 erase_line(2,2,40);
  305.               dirset(tmp);
  306.                 disp_menu(menu,n_ent);
  307.                    break;
  308.                    case '4':
  309.               erase_menu(n_ent);
  310.               dirset(tmp);
  311.               ask_what(buffer,2);
  312.               if((strlen(buffer)) != 0){
  313.                 GetArgs(buffer);
  314.                 /* Sprintf(string,"%s %s",menu[tmp].cmd,buffer); */
  315.                 exec_cshell(string,CONTINUE);
  316.                 clear_array(buffer);
  317.                 clear_array(string);
  318.               }else
  319.                 erase_line(2,2,40);
  320.               dirset(tmp);
  321.                 disp_menu(menu,n_ent);
  322.                    break;
  323.                case '5':
  324.               erase_menu(n_ent);
  325.               dirset(tmp);
  326.               ptr = do_selection(menu[tmp].AllowWander);
  327.               if(ptr != NULL){
  328.                 GetArgs(ptr);
  329.                 /* Sprintf(string,"%s %s",menu[tmp].cmd,ptr); */
  330.                 exec_cshell(string,CONTINUE);
  331.                 clear_array(buffer);
  332.                 clear_array(string);
  333.               }
  334.               dirset(tmp);
  335.               disp_menu(menu,n_ent);
  336.                break;
  337.                case '6':
  338.               erase_menu(n_ent);
  339.               dirset(tmp);
  340.               ptr = do_selection(menu[tmp].AllowWander);
  341.               if(ptr != NULL){
  342.                 GetArgs(ptr);
  343.                 /* Sprintf(string,"%s %s",menu[tmp].cmd,ptr); */
  344.                 exec_pipe(string);
  345.                 clear_array(buffer);
  346.                 clear_array(string);
  347.               }
  348.               dirset(tmp);
  349.               disp_menu(menu,n_ent);
  350.                break;
  351.                    default:
  352.                break;
  353.          }
  354.        }
  355.       }
  356.       move_csr(0,23);
  357.       inp = my_getchar();
  358.       if(inp == 'h'){
  359.          inp = disp_help();
  360.          if(inp != 'x' && inp!= 'd' && inp != 'c' && inp!=RE_DRAW)
  361.            disp_menu(menu,n_ent);
  362.       }
  363.     }
  364.     quit();
  365. }
  366.  
  367. /*
  368.  *    static erase_menu():
  369.  *
  370.  *    Parameters:    num - the number of entries that need
  371.  *            to be erased.
  372.  *    
  373.  *    Purpose:    Erase the current menu off the screen.
  374.  *
  375.  *    Returns:    None
  376.  *
  377.  *    Last Modify:    01-12-91 (TW)
  378.  *
  379.  */
  380.  
  381. static erase_menu(num)
  382. int num;
  383. {
  384.     int ind = 0;
  385.     char buff[50];
  386.     
  387.     Sprintf(buff,"%40s"," ");
  388.     move_csr(20,3);
  389.     print_str(buff);
  390.     for(ind=0;ind<num;ind++){
  391.        move_csr(NUMCOL,(ind*2) + 4);
  392.        print_str(buff);
  393.     }
  394. }
  395.  
  396. /*
  397.  *    static display_help():
  398.  *
  399.  *    Parameters:     None
  400.  *    
  401.  *    Purpose:    Display the Help Window.
  402.  *
  403.  *    Returns:    None
  404.  *
  405.  *    Last Modify:    01-12-91 (TW)
  406.  *
  407.  */
  408.  
  409. static disp_help()
  410. {
  411.     char buff[40];
  412.     int i;
  413.     char Key;
  414.     
  415.     Sprintf(buff,"%30s"," ");
  416.     move_csr(25,3);
  417.     hi_lite(buff);
  418.     for(i=4;i<=19;i++){
  419.        move_csr(25,i);
  420.        print_str(buff);
  421.        start_rev();
  422.        move_csr(25,i);
  423.        outc(' ');
  424.        move_csr(54,i);
  425.        outc(' ');
  426.        end_rev();
  427.     }
  428.     move_csr(25,19);
  429.     print_str(buff);
  430.     move_csr(25,19);
  431.     hi_lite(buff);
  432.     move_csr(27,4);
  433.     print_str("Command Summary:");
  434.     move_csr(27,6);
  435.     print_str("(q)       Quit Menu");
  436.     move_csr(27,7);
  437.     print_str("(x)       Execute Command");
  438.     move_csr(27,8);
  439.     print_str("(c)       Change Directory");
  440.     move_csr(27,9);
  441.     print_str("(d)       Show Directory");
  442.     move_csr(27,10);
  443.     print_str("(l)       Long Directory");
  444.     move_csr(27,11);
  445.     print_str("(?)       Get Help on Item");
  446.     move_csr(27,12);
  447.     print_str("(m)       Go to Main Menu");
  448.     move_csr(27,13);
  449.     print_str("(p)       Go to Prev Menu");
  450.     move_csr(27,14);
  451.     print_str("(P)       Print a file.");
  452.     move_csr(27,15);
  453.     print_str("(L)       Log off!");
  454.     move_csr(27,16);
  455.     print_str("(ctrl-R)  Redraw Screen.");
  456.     move_csr(27,18);
  457.     Key = my_getchar();
  458.     erase_help();
  459.     return(Key);
  460. }
  461.  
  462. /*
  463.  *    static erase_help():
  464.  *
  465.  *    Parameters:    None
  466.  *    
  467.  *    Purpose:    Erase The Help Window.
  468.  *
  469.  *    Returns:    None
  470.  *
  471.  *    Last Modify:    01-12-91 (TW)
  472.  *
  473.  */
  474.  
  475. static erase_help()
  476. {
  477.     char buff[40];
  478.     int i;
  479.  
  480.     Sprintf(buff,"%30s"," ");
  481.     for(i=3;i<=19;i++){
  482.        move_csr(25,i);
  483.        print_str(buff);
  484.     }
  485. }
  486.  
  487. /*
  488.  *    void chop_str():
  489.  *
  490.  *    Parameters:    instring   - A string to chop to 70 columns
  491.  *                max_length - max length for string.
  492.  *    
  493.  *    Purpose:    Strip anything after the 70th column off.
  494.  *
  495.  *    Returns:    None.
  496.  *
  497.  *    Last Modify:    06-10-91 (TW)
  498.  *
  499.  */
  500.  
  501. void chop_str(instring,max_length)
  502. char *instring;
  503. int max_length;
  504. {
  505.     int length,i;
  506.     
  507.     length = strlen(instring);
  508.     while(*instring != '\0')
  509.        *instring++;
  510.     for(i=length;i>max_length;i--,*instring--)
  511.       *instring = '\0';
  512. }
  513.  
  514. void pad_str(instring)
  515. char *instring;
  516. {
  517.     int length,i;
  518.     
  519.     length = strlen(instring);
  520.     for(i=length;i<=70;i++)
  521.        instring[i] = ' ';
  522.     instring[70] = '\0';
  523. }
  524.  
  525. /*
  526.  *    static do_help():
  527.  *
  528.  *    Parameters:    name - String containing a file name.
  529.  *            y    - Number of lines from top of screen.
  530.  *    
  531.  *    Purpose:    read a file name into the string.
  532.  *
  533.  *    Returns:    None.
  534.  *
  535.  *    Last Modify:    01-12-91 (TW)
  536.  *
  537.  */
  538.  
  539. static One_fname(name,y)
  540. char *name;
  541. int y;
  542. {
  543.     move_csr(2,y);
  544.     clear_array(name);
  545.     print_str("Filename: ");
  546.     read_str(name,30);
  547. }
  548.  
  549. /*
  550.  *    static Two_fname():
  551.  *
  552.  *    Parameters:    name - String containing a file name.
  553.  *            y    - Number of lines from top of screen.
  554.  *    
  555.  *    Purpose:
  556.  *
  557.  *    Returns:
  558.  *
  559.  *    Last Modify:    01-12-91 (TW)
  560.  *
  561.  */
  562.  
  563. static Two_fname(name,y)
  564. char *name;
  565. int y;
  566. {
  567.     move_csr(2,y);
  568.     clear_array(name);
  569.     print_str("New File: ");
  570.     read_str(name,30);
  571. }
  572.  
  573. /*
  574.  *    static change():
  575.  *
  576.  *    Parameters:    None.
  577.  *    
  578.  *    Purpose:    Change to a new working directory.
  579.  *
  580.  *    Returns:    None.
  581.  *
  582.  *    Last Modify:    01-12-91 (TW)
  583.  *
  584.  */
  585.  
  586. static change()
  587. {
  588.     move_csr(2,2);
  589.     clear_array(buffer);
  590.     print_str("Directory: ");
  591.     read_str(buffer,30);
  592.     (void) chdir(buffer);
  593.     erase_line(2,2,60);
  594.     prt_curdir();
  595.     clear_array(buffer);
  596. }
  597.  
  598. /*
  599.  *    static exec_it():
  600.  *
  601.  *     Parameters:    None.
  602.  *    
  603.  *    Purpose:    Execute A shell commenad.
  604.  *
  605.  *    Returns:    None.
  606.  *
  607.  *    Last Modify:    01-12-91 (TW)
  608.  *
  609.  */
  610.  
  611. static exec_it()
  612. {
  613.     move_csr(2,2);
  614.     print_str("Command: ");
  615.     clear_array(buffer);
  616.     read_str(buffer,30);
  617.     exec_cshell(buffer,CONTINUE);
  618. }
  619.  
  620. /*
  621.  *    static ask_who():
  622.  *
  623.  *    Parameters:    None.
  624.  *    
  625.  *    Purpose:    Ask for a login name.
  626.  *
  627.  *    Returns:    None.
  628.  *
  629.  *    Last Modify:    01-12-91 (TW)
  630.  *
  631.  */
  632.  
  633. static ask_who(name,y)
  634. char *name;
  635. int y;
  636. {
  637.     move_csr(2,y);
  638.     clear_array(name);
  639.     print_str("To Whom: ");
  640.     read_str(name,30);
  641.     move_csr(2,3);
  642.     Fflush(stdout);
  643. }
  644.  
  645. /*
  646.  *    static ask_what():
  647.  *
  648.  *    Parameters:    name - Name of what you are asking.
  649.  *            y    - number of rows down from top of screen.
  650.  *    
  651.  *    Purpose:    Ask for a topic.
  652.  *
  653.  *    Returns:    None.
  654.  *
  655.  *    Last Modify:    01-12-91 (TW)
  656.  *
  657.  */
  658.  
  659. static ask_what(name,y)
  660. char *name;
  661. int y;
  662. {
  663.     move_csr(2,y);
  664.     clear_array(name);
  665.     print_str("Topic: ");
  666.     read_str(name,30);
  667. }
  668.  
  669. /*
  670.  *    void check_dirname():
  671.  *
  672.  *    Parameters:    string - Name of directory.
  673.  *    
  674.  *    Purpose:    Expand '~' to users Home Directory.
  675.  *
  676.  *    Returns:    None.
  677.  *
  678.  *    Last Modify:    01-12-91 (TW)
  679.  *
  680.  */
  681.  
  682. void check_dirname(string)
  683. char *string;
  684. {
  685.     char *tmp_str;
  686.  
  687.     tmp_str = (char *) malloc(80);;
  688.     if(*string == '~'){
  689.       Strcpy(tmp_str,getenv("HOME"));
  690.       Strcat(tmp_str,string+1);
  691.       Strcat(tmp_str,"/");
  692.       string = tmp_str;
  693.     }
  694. }
  695.